package com.itboye.ihomebank.home;

import java.io.File;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.AvatarDataBeans;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.HuaBanView;
import com.itboye.ihomebank.util.SaveViewUtil;
import com.itboye.ihomebank.util.UploadImage;
import com.itboye.ihomebank.util.UploadImage.OnUploadlistener;

public class ActivityHuaBan extends BaseOtherActivity {
	//
	HuaBanView huaban;
	String fileName = "";
	ImageView close_icon;
	TextView add_shap_title_tv;
	Button ok, reset;
	public static File rootDir = new File(
			Environment.getExternalStorageDirectory() + File.separator
					+ "huaban/");

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.reset:
			huaban.clearScreen();
			break;
		case R.id.ok:
			Long currentMil = System.currentTimeMillis();
			if (SaveViewUtil.saveScreen(huaban, currentMil)) {
				 Toast.makeText(this, "保存画板成功", 0).show();
				UploadImage.getInstance().execute(
						new String[] { rootDir.getAbsolutePath() + "/"
								+ currentMil + ".jpg" }, "signature",
						new OnUploadlistener() {

							@Override
							public void OnSuccess(
									ArrayList<AvatarDataBeans> resultData) {
								// TODO Auto-generated method stub
								ByAlert.alert("签名已上传");
								Log.v("imgSign", resultData.get(0).getId());
								Intent intent = new Intent();
								intent.putExtra("imgId", resultData.get(0)
										.getId());
								setResult(108,intent);
								finish();
							}

							@Override
							public void OnError(String resultData) {
								// TODO Auto-generated method stub
								ByAlert.alert(resultData);
							}
						}, null);
			} else {
				Toast.makeText(this, "保存失败，请检查sdcard是否可用", 0).show();
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_huabanqianming;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("填写签名");
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

}
